package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.app.Dialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DrawableTextView;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SecurityCodeView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/8/1 15:50
 * 邮箱：zhanghuaiha@gmail.com
 */

public class PassWordActivity extends BaseActivity{
    private TextView one,two,three,four,five,six;
    private DrawableTextView error;
    private Dialog dialog;
    private View save;
    private SecurityCodeView edittext;
    private ImageView back;
    private String alias;
    private String versionCode;
    private String id;
    private String udid;
    private Dialog upload;

    @Override
    public void initView() {
        one = (TextView) findViewById(R.id.pw_one);
        two = (TextView) findViewById(R.id.pw_two);
        three = (TextView) findViewById(R.id.pw_three);
        four = (TextView) findViewById(R.id.pw_four);
        five = (TextView) findViewById(R.id.pw_five);
        six = (TextView) findViewById(R.id.pw_six);
        error = (DrawableTextView) findViewById(R.id.pw_error);
        back = (ImageView) findViewById(R.id.back);
    }

    @Override
    public void initData() {
        versionCode = ObtainVersion.getVersionName(this);
        id = App.SP.getString("id", "");
        udid = ObtainUdis.getUDID(PassWordActivity.class);
        ArrayList<Integer> list = getIntent().getIntegerArrayListExtra("List");
        alias = getIntent().getStringExtra("alias");
        dialog = DialogUtils.inputDialog(this, false);
        save = dialog.findViewById(R.id.dialog_save);
        edittext = (SecurityCodeView) dialog.findViewById(R.id.scv_edittext);
        one.setText(list.get(0)+"");
        two.setText(list.get(1)+"");
        three.setText(list.get(2)+"");
        four.setText(list.get(3)+"");
        five.setText(list.get(4)+"");
        six.setText(list.get(5)+"");
    }

    @Override
    public void initListener() {
        error.setOnClickListener(v -> {
            edittext.clearEditText();
            dialog.show();
        });
        save.setOnClickListener(v -> {
            String editContent = edittext.getEditContent();
            if (editContent != null || editContent.length() >= 2) {
                loadData(editContent);
            }else {
                ToastUtil.showToast("输入的字符不正确");
            }
            dialog.dismiss();
        });
        edittext.setInputCompleteListener(new SecurityCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
            }

            @Override
            public void deleteContent(boolean isDelete) {

            }
        });

        back.setOnClickListener(v -> finish());
    }


    public void loadData(String step){
        uploadDialog();
        String tiem = System.currentTimeMillis() + "";
        Map<String,String> map = new LinkedHashMap<>();
        map.put("rf","2");
        map.put("ts",tiem);
        map.put("v", versionCode);
        map.put("i", NetWorkCodeInfo.GET_PASSWORD);
        map.put("udid",udid);
        map.put("eid", id);
        map.put("alias",alias);
        map.put("step",step);
        String encry1 = ObtainEncry.getEncry(map);
        map.put("encry",encry1);
        RetrofitFactory.getmInstanse().mApi.again_password(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<Integer>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<Integer>> listBaseResponseEntity) {
                        if (listBaseResponseEntity.code == 200) {
                            List<Integer> list = listBaseResponseEntity.data;
                            one.setText(list.get(0)+"");
                            two.setText(list.get(1)+"");
                            three.setText(list.get(2)+"");
                            four.setText(list.get(3)+"");
                            five.setText(list.get(4)+"");
                            six.setText(list.get(5)+"");
                        }else {
                            DialogUtils.TSDialog(PassWordActivity.this,listBaseResponseEntity.message,true);
                        }
                        upload.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        DialogUtils.TSDialog(PassWordActivity.this,PassWordActivity.this.getString(R.string.error_date),true);
                        upload.dismiss();
                    }
                });
    }

    public void uploadDialog(){
        upload = DialogUtils.uploadDialog(this, this.getString(R.string.upload), false);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_pass_word;
    }

}
